﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Resources;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinApp.Properties;

namespace WinApp.i18n
{
    public static class LanguageType
    {
        public const string English = "EN";
        public static string China = "CN";
    }
    public static class Language
    {
        private static ResourceManager _rm;
        private static string langFile = "lang.txt";
        static Language()
        {
            if (_rm == null)
            {
                // 创建资源管理器
                if (GlobalVariables.SystemLanguage == LanguageType.China)
                {
                    UILocalizeHelper.SetCH();
                    _rm = new ResourceManager("WinApp.i18n.Lang_CH", typeof(Lang_CH).Assembly);
                }
                else
                {
                    UILocalizeHelper.SetEN();
                    _rm = new ResourceManager("WinApp.i18n.Lang_EN", typeof(Lang_EN).Assembly);
                }

            }

        }

        public static string GetLang(string strKey)
        {
            var result = _rm.GetString(strKey);
            return result == null ? "" : result;
        }


        public static string InitLang()
        {
            var result = string.Empty;
            if (System.IO.File.Exists(langFile))
            {
                result = System.IO.File.ReadAllText(langFile);//读取文件;
            }
            else
            {//默认英文
                System.IO.File.WriteAllText(langFile, LanguageType.English);//写入文件                
            }
            return result;
        }

        public static void SetLang(string lang)
        {
            System.IO.File.WriteAllText(langFile, lang);
        }
    }
}
